package com.kaiyidiannao.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.TourBean;
import com.kaiyidiannao.model.req.model.GetCloseOpen;
import com.kaiyidiannao.view.ScaleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 14-4-22.
 * 凑队去 - 微记
 */
public class MyTourSmallNoteAdapter extends ImageLoaderBaseAdapter {
    private ImageLoader imageLoader;
    private LayoutInflater layoutInflater;
    private List<TourBean> list = new ArrayList<TourBean>();
    private String[] imgs;
    private Context context;
    private int tip = 0;

    public MyTourSmallNoteAdapter(Context context, List<TourBean> list,int tip) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.tip = tip;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ScaleImageView imageView;

        TextView tour_small_note_nameTextView;
        TextView tour_small_note_item_add_data;
        ImageButton mySmallNoteItemButton;

        TourBean bean =  list.get(position);
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.my_tour_small_note_item, null);
        }
        imageView =  (ScaleImageView) convertView.findViewById(R.id.tour_small_note_item_ImageViews);
        tour_small_note_nameTextView = (TextView) convertView.findViewById(R.id.tour_small_note_nameTextView);
        tour_small_note_item_add_data = (TextView) convertView.findViewById(R.id.tour_small_note_item_add_data);
        mySmallNoteItemButton = (ImageButton)convertView.findViewById(R.id.mySmallNoteItemButton);


        tour_small_note_nameTextView.setText(bean.getTitle());
        tour_small_note_item_add_data.setText(bean.getAddtime());
//        if (bean.getAddtime() == null){
//            tour_small_note_item_add_data.setText("无");
//        }else{
//            try {
//                String dates = new SimpleDateFormat("yyyy-MM-dd").format(new Date(Long.parseLong(bean.getAddtime())));
//                Log.d("dates",dates);
//                tour_small_note_item_add_data.setText(dates);
//            } catch (NumberFormatException e) {
//                tour_small_note_item_add_data.setText(bean.getAddtime().substring(0, bean.getAddtime().indexOf(" ")));
//                e.printStackTrace();
//            }
//        }
//        imgs = (bean.getThumb()+",").split(",");
//        imageLoader.displayImage(imgs[0], imageView);
        imageLoader.displayImage(bean.getThumb(),imageView, options, null);
        if(list.get(position).getStatus()!=null){
            if (list.get(position).getStatus().equals("2")) {
                mySmallNoteItemButton.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_dark_button));
            }else {
                mySmallNoteItemButton.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_light_button));
            }

        }
        if (null != convertView) {
            if (null != mySmallNoteItemButton) {
                mySmallNoteItemButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DataCenter.position = position;
                        ((BaseActionBarActivity)context).showProgressDialog();
                        if (Integer.parseInt(list.get(position).getStatus()) == 2) {
                            new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "open", DataCenter.beansSmall.get(position).getItemid(), "31",DataCenter.tour_category_id[1]+"").execute();
                        } else if (Integer.parseInt(list.get(position).getStatus()) == 3) {
                            new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "close", DataCenter.beansSmall.get(position).getItemid(), "31",DataCenter.tour_category_id[1]+"").execute();
                        }
                    }
                });
            }
        }

        return convertView;
    }
}
